﻿using Furion.DatabaseAccessor;
using Furion.JsonSerialization;
using Htx.Fur.Core.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;

namespace Htx.Fur.EntityFramework.Core
{
    [AppDbContext("SqlserverConnectionString", DbProvider.SqlServer)]
    public class DefaultDbContext : AppDbContext<DefaultDbContext>
    {
        public DefaultDbContext(DbContextOptions<DefaultDbContext> options) : base(options)
        {
            this.SavedChanges += (sender, args) =>
            {
                var entities = this.ChangeTracker.Entries<UserInfo>();
                foreach (var item in entities)
                {
                    System.Console.WriteLine($"更新 {nameof(UserInfo)} 表，实体状态：{item.State}，对象：{JSON.Serialize(item.Entity)}");
                }
            };
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            // 配置实体懒加载
            //optionsBuilder.UseLazyLoadingProxies();

            base.OnConfiguring(optionsBuilder);

            
        }

    }
}